package org.example.auth.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.example.api.psn.IPersonServiceFeignClient;
import org.example.auth.core.entity.Account;
import org.example.auth.core.service.IAccountService;
import org.example.auth.form.PwdLoginForm;
import org.example.system.common.utils.ResponseUtil;
import org.example.system.common.utils.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 授权管理-密码模式
 * @author joey
 */
@ApiSort(1)
@Api(tags = "1、授权管理")
@Slf4j
@RestController
public class PwdAuthController {
    @Resource
    IAccountService accountService;
    @Resource
    IPersonServiceFeignClient personServiceFeignClient;

    @ApiOperationSupport(order = 111)
    @ApiOperation(value = "A111-密码模式-登陆")
    @PostMapping("/pwd/login")
    public ResponseUtil pwdLogin(@Validated PwdLoginForm form){
        try {
            ResponseUtil<String> res = personServiceFeignClient.queryNamesById("dd");
            System.out.println(res);
            return accountService.pwdLogin(form);
        }catch (Exception e){
            log.error("A111密码模式-登陆-出错",e);
            return ResponseUtil.error();
        }
    }


    @ApiOperationSupport(order = 113)
    @ApiOperation(value = "A113-密码模式-检测帐号名是否已存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name="accountNum",value="帐号名",dataType = "string", paramType = "query",required = true),
            @ApiImplicitParam(name="accountId",value="帐号id",dataType = "long", paramType = "query",required = false)
    })
    @GetMapping("/pwd/existNum")
    public ResponseUtil existAccountNum(
            @NotNull(message = "帐号名不能为空")String accountNum,
            Long accountId){
        try {
            Account account = accountService.queryByName(accountNum.trim());
            if(account == null){
                return ResponseUtil.success(false);
            }else {
                if(accountId!=null && accountId.equals(account.getId())){
                    return ResponseUtil.success(false);
                }
                return ResponseUtil.success(true);
            }
        }catch (Exception e){
            log.error("A113-密码模式-检测帐号名是否已存在-出错",e);
            return ResponseUtil.error();
        }
    }



}
